// 前端请求拦截器
import axios from 'axios';
import { message } from 'ant-design-vue';

const service = axios.create({
  baseURL: 'http://localhost:5000/api',
  timeout: 10000
});

// 请求拦截器
service.interceptors.request.use(config => {
  const token = localStorage.getItem('token');
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

// 响应拦截器（合并重复定义）
service.interceptors.response.use(
  response => {
    if (response.data?.code === 200) {
      return response.data;
    }
    return Promise.reject(response.data);
  },
  error => {
    if (error.response?.status === 401) {
      message.error('登录状态已过期，请重新登录');
      localStorage.removeItem('token');
      window.location.href = '/login';
    }
    return Promise.reject(error);
  }
);

export default service;
